//
//  ShoppingFenLeiViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/7/5.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "ShoppingFenLeiViewController.h"
#import "NavView.h"
#import "FenLeiLeftTableViewCell.h"
#import "FenLeiCollectionView.h"
#import "CommodityDetailsViewController.h"
@interface ShoppingFenLeiViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate>{
    NSMutableArray*oneArrData;
}

@property(nonatomic,strong)NavView*navView;
@property(nonatomic,strong)UITableView*lestTabView;
@property(nonatomic,strong)NSArray*arr;
@property(nonatomic,strong)FenLeiCollectionView*collectionView;
@end

@implementation ShoppingFenLeiViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
}
-(void)oneHttp
{
    [HTTPURL postRequest:LINK_BASE_URL(@"/goods/goodCategory.api") parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary*dic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        
        NSLog(@"%@",dic);
        _arr=(NSArray*)dic;
        [_arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString*name=obj[@"name"];
            [oneArrData addObject:name];
        }];
        NSLog(@"%@",oneArrData);
        [_lestTabView reloadData];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}
-(void)twoHttp
{
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    oneArrData=[NSMutableArray new];
    [self oneHttp];
    self.view.backgroundColor=LRRGBColor(235, 240, 243);
    [self setLestUI];
    self.automaticallyAdjustsScrollViewInsets=NO;
   
    _navView=[[NavView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 64)];
    [_navView.backButton addTarget:self action:@selector(popButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_navView];
    [self.view addSubview:self.collectionView];

}
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 设置collectionView的滚动方向，需要注意的是如果使用了collectionview的headerview或者footerview的话， 如果设置了水平滚动方向的话，那么就只有宽度起作用了了
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        // layout.minimumInteritemSpacing = 10;// 垂直方向的间距
        // layout.minimumLineSpacing = 10; // 水平方向的间距
        _collectionView = [[FenLeiCollectionView alloc] initWithFrame:CGRectMake(90, 64, width_screen-110, height_screen-64) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = _collectionView;
        _collectionView.delegate = self;
    }
    return _collectionView;
}
#pragma mark -- UICollectionViewDelegateFlowLayout
/** 每个cell的尺寸*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((width_screen-130)/3, (width_screen-100)/3+20);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommodityDetailsViewController*vc=[CommodityDetailsViewController new];
    vc.ID=self.collectionView.dataArr[indexPath.row][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)popButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setLestUI
{

    _lestTabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 80, height_screen) style:0];
    _lestTabView.separatorStyle=0;
    _lestTabView.delegate=self;
    //    tabView.mj_header
    _lestTabView.dataSource=self;
    [_lestTabView registerClass:[FenLeiLeftTableViewCell class] forCellReuseIdentifier:@"leftcell"];
    _lestTabView.layer.borderWidth=1;
    _lestTabView.layer.borderColor=LRRGBColor(235, 240, 243).CGColor;
    [self.view addSubview:_lestTabView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FenLeiLeftTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"leftcell"];
    
    cell.lab.text=oneArrData[indexPath.row];
   
   

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 47;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return oneArrData.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",_arr[indexPath.row][@"id"]);
    NSString*idStr=[NSString stringWithFormat:@"%ld",[_arr[indexPath.row][@"id"]integerValue]];
   
    [HTTPURL postRequest:LINK_BASE_URL(@"/goods/goodCategorySub.api") parameters:@{@"categoryId":idStr} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"retFlag"]integerValue]==1) {
        NSDictionary*dic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        self.collectionView.dataArr=(NSMutableArray*)dic;
        [self.collectionView reloadData];
        }
        
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
